package org.example.ui;

import com.formdev.flatlaf.FlatDarkLaf;
import org.example.ui.SoftManagementPanel;  // 新增导入

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MainFrame extends JFrame {
    JFrame jFrame = this;
    private CardLayout cardLayout;
    private JPanel contentPanel;
    private JLabel promptLabel;

    public MainFrame() {
        // 设置外观
        try {
            UIManager.setLookAndFeel(new FlatDarkLaf());
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }

        setUndecorated(true);
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 设置全局字体和颜色
        UIManager.put("Tree.background", new Color(50, 50, 50));
        UIManager.put("Tree.selectionBackground", new Color(70, 70, 70));
        UIManager.put("Tree.textForeground", Color.WHITE);
        UIManager.put("Tree.font", new Font("微软雅黑", Font.PLAIN, 16));

        // 标题栏
        JPanel header = new JPanel();
        header.setBackground(new Color(30, 30, 30));
        JLabel lblTitle = new JLabel("企 业 员 工 管 理 系 统");
        lblTitle.setFont(new Font("隶书", Font.BOLD, 72));
        lblTitle.setForeground(Color.WHITE);
        header.add(lblTitle);
        add(header, BorderLayout.NORTH);

        // 创建树节点
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("菜单");

        // 添加员工管理及其子菜单
        DefaultMutableTreeNode employeeManagement = new DefaultMutableTreeNode("员工管理");
        employeeManagement.add(new DefaultMutableTreeNode("查看员工"));
        employeeManagement.add(new DefaultMutableTreeNode("添加员工"));
        employeeManagement.add(new DefaultMutableTreeNode("删除员工"));
        root.add(employeeManagement);

        // 添加部门管理及其子菜单
        DefaultMutableTreeNode departmentManagement = new DefaultMutableTreeNode("部门管理");
        departmentManagement.add(new DefaultMutableTreeNode("查看部门"));
        departmentManagement.add(new DefaultMutableTreeNode("添加部门"));
        root.add(departmentManagement);

        // 添加项目管理及其子菜单（关键部分）
        DefaultMutableTreeNode softManagement = new DefaultMutableTreeNode("项目管理");
        softManagement.add(new DefaultMutableTreeNode("查看项目"));
        softManagement.add(new DefaultMutableTreeNode("添加项目"));
        softManagement.add(new DefaultMutableTreeNode("删除项目"));
        root.add(softManagement);

        // 添加考勤管理及其子菜单
        DefaultMutableTreeNode AttendanceManagement = new DefaultMutableTreeNode("考勤管理");
        AttendanceManagement.add(new DefaultMutableTreeNode("查看或添加考勤"));

        // 添加退出节点
        root.add(new DefaultMutableTreeNode("退出"));

        JTree tree = new JTree(root);
        tree.setCellRenderer(new CustomTreeCellRenderer());
        tree.setRowHeight(30); // 设置行高
        tree.setForeground(Color.WHITE);
        tree.setBackground(new Color(50, 50, 50));

        // 监听点击事件，切换卡片内容
        tree.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                TreePath path = tree.getPathForLocation(e.getX(), e.getY());
                if (path != null) {
                    DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
                    String selectedMenu = node.getUserObject().toString();

                    switch (selectedMenu) {
                        case "退出":
                            int result = JOptionPane.showConfirmDialog(jFrame, "确认退出吗？");
                            if (result == JOptionPane.OK_OPTION) {
                                System.exit(0);
                            }
                            break;

                        // 员工管理相关
                        case "查看员工":
                            cardLayout.show(contentPanel, "EmployeeManagementPanel");
                            break;
                        case "添加员工":
                            cardLayout.show(contentPanel, "EmployeeManagementPanel1");
                            break;
                        case "删除员工":
                            cardLayout.show(contentPanel, "EmployeeManagementPanel2");
                            break;

                        // 部门管理相关
                        case "查看部门":
                            cardLayout.show(contentPanel, "DepartmentManagementPanel");
                            break;
                        case "添加部门":
                            cardLayout.show(contentPanel, "DepartmentManagementPanel1");
                            break;

                        // 项目管理相关（关键部分）
                        case "查看项目":
                            cardLayout.show(contentPanel, "SoftManagementPanel");
                            break;
                        case "添加项目":
                            cardLayout.show(contentPanel, "SoftManagementPanel"); // 假设在同一面板处理
                            break;
                        case "删除项目":
                            cardLayout.show(contentPanel, "SoftManagementPanel"); // 假设在同一面板处理
                            break;

                        //考勤管理相关
                        case "查看或添加考勤":
                            cardLayout.show(contentPanel, "AttendanceManagementPanel"); // 假设在同一面板处理
                            break;
                    }
                }
            }
        });

        JScrollPane menuScrollPanel = new JScrollPane(tree);
        menuScrollPanel.setBorder(BorderFactory.createEmptyBorder());
        menuScrollPanel.getViewport().setBackground(new Color(50, 50, 50));

        // 卡片布局（关键部分）
        cardLayout = new CardLayout();
        contentPanel = new JPanel(cardLayout);
        contentPanel.setBackground(new Color(40, 40, 40));
        promptLabel = new JLabel("请在业务菜单中选中要执行的业务", SwingConstants.CENTER);
        promptLabel.setFont(new Font("微软雅黑", Font.PLAIN, 24));
        promptLabel.setForeground(Color.WHITE);
        contentPanel.add(promptLabel, "prompt");
        contentPanel.add(new EmployeeManagementPanel(), "EmployeeManagementPanel");
        contentPanel.add(new EmployeeManagementPanel1(), "EmployeeManagementPanel1");
        contentPanel.add(new EmployeeManagementPanel2(), "EmployeeManagementPanel2");
        contentPanel.add(new DepartmentManagementPanel(), "DepartmentManagementPanel");
        contentPanel.add(new SoftManagementPanel(), "SoftManagementPanel"); // 添加项目管理面板
        contentPanel.add(new AttendanceManagementPanel(), "AttendanceManagementPanel");

        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, menuScrollPanel, contentPanel);
        splitPane.setDividerLocation(200);
        splitPane.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1, new Color(60, 60, 60)));

        add(splitPane, BorderLayout.CENTER);
        setVisible(true);
    }

    static class CustomTreeCellRenderer extends DefaultTreeCellRenderer {
        private final Icon folderIcon = UIManager.getIcon("FileView.directoryIcon");
        private final Icon fileIcon = UIManager.getIcon("FileView.fileIcon");

        @Override
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded,
                                                      boolean leaf, int row, boolean hasFocus) {
            super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
            if (value instanceof DefaultMutableTreeNode) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
                if (node.getChildCount() > 0) {
                    setIcon(folderIcon);
                } else {
                    setIcon(fileIcon);
                }
            }
            setBackgroundNonSelectionColor(new Color(50, 50, 50));
            setTextSelectionColor(Color.WHITE);
            return this;
        }
    }
}